<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/
class ClsLCsv extends ClsNaanalLibrary
{
    public $arrRowHead=null;
    private $isHead=false;
    public $arrColumnHead=null;
    public $arrFormInputData=null;

    private $rows=0;
    private $cols=0;
    
    public function __construct()
    {
        
    }
    public function getRowCount()
    {
        return $this->rows;
    }
    public function getColumnCount()
    {
        return $this->cols;
    }
    function open($filename,$isHead=false,$callback=null,$arrHeadMap=null)
    {
        $this->isHead=$isHead;
        $row = -1;
        if (($handle = fopen($filename, "r")) !== FALSE) 
        {
            $arrIgnoreColumn=array();
            if($isHead)
            {
                if (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
                {
                    $num = count($data);
                    $newdata=array();
                    if(!is_null($arrHeadMap))
                    {
                        for ($c=0; $c < $num; $c++)
                        {
                            if(isset($arrHeadMap[$data[$c]]))
                            {
                                $newdata[]=$arrHeadMap[$data[$c]]; 
                            }
                            else
                            {
                                $arrIgnoreColumn[]=$c;
                            }
                        }
                    }
                    else
                    {
                        for ($c=0; $c < $num; $c++)
                        {
                                $newdata[]=$data[$c]; 
                        }
                    }
                    if(is_null($callback))
                    {
                        $num = count($newdata);
                        for ($c=0; $c < $num; $c++)
                        {
                            $this->arrColumnHead[$c]=$newdata[$c]; 
                        }
                    }
                    else
                    {
                        $callback($newdata,true);
                    }
                }
            }
            while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) 
            {
                $num = count($data);
                if(is_null($callback))
                {
                    $row++;
                    for ($c=0; $c < $num; $c++) {
                        if(in_array($c, $arrIgnoreColumn)) continue;
                        $this->arrFormInputData[$row][]=addslashes($data[$c]); 
                    }
                }
                else
                {
                    $newdata=array();
                    for ($c=0; $c < $num; $c++) {
                        if(in_array($c, $arrIgnoreColumn)) continue;
                        $newdata[]=$data[$c];
                    }
                    $callback($newdata,false);
                }
            }
            fclose($handle);
        }   //exit;
            //if($isHead) $this->arrColumnHead=array_shift($this->arrFormInputData);
        $this->rows=!empty($this->arrFormInputData)?count($this->arrFormInputData):0;
        $this->cols=(isset($this->arrFormInputData[0]))?count($this->arrFormInputData[0]):0;
    }

    function setColumnField()
    {
            if(!is_null($this->arrRowHead)) return;
            array_shift($this->arrColumnHead);
            foreach($this->arrFormInputData as $ind=>$arrRow)
            {
                    $item=array_shift($arrRow);
                    if(!is_null($this->arrRowHead) && in_array($item,$this->arrRowHead)) 
                            die("duplicate entry exist in csv file at row ".($this->isHead?$ind+2:$ind+1).", item:".$item." in rowhead");
                    $this->arrRowHead[]=$item;
                    $this->arrFormInputData[$ind]=$arrRow;
            }
            $this->cols=$this->cols-1;
    }
    ///gets the row index of data based on the column specified.
    function getRowIndex($col,$data)
    {
            for($i=0;$i<$this->rows;$i++)
            {
                    if($this->getData($i,$col)==$data) return $i;
            }
            return -1;
    }
    function getRow($rowindex)
    {
            $arr=null;
            for($i=0;$i<count($this->arrColumnHead);$i++)
            {
                    $arr[$this->arrColumnHead[$i]]=$this->getData($rowindex,$this->arrColumnHead[$i]);	
            }	
            return $arr;
    }
    function getAllRows()
    {
            $arr=null;
            for($i=0;$i<$this->rows;$i++)
            {
                    $arr[]=$this->getRow($i);	
            }	
            return $arr;
    }
    function getData($row,$col)
    {//echo "-$row,$col-";
            if(is_int($row) && is_int($col))
            {
                    return $this->arrFormInputData[$row][$col];
            }
            else if(is_int($row))
            {
                    if($row>=$this->rows) die("invalid row number ".$row." where only ".$this->rows." only exist");
                    $ind=array_search($col,$this->arrColumnHead);
                    if($ind!==FALSE)
                    {
                            return $this->arrFormInputData[$row][$ind];
                    }
                    else
                    {
                            return null;
                    }
            }
            else if(is_int($col))
            {
                    if($col>=$this->cols) die("invalid row number ".$col." where only ".$this->cols." only exist");
                    $ind=array_search($row,$this->arrRowHead);
                    if($ind!==FALSE)
                    {
                            return $this->arrFormInputData[$ind][$col];
                    }
                    else
                    {
                            die("invalid request");
                    }
            }
            else
            {
                    $col=array_search($col,$this->arrColumnHead);
                    $row=array_search($row,$this->arrRowHead);
                    return $this->arrFormInputData[$row][$col];
            }
    }
}
?>